﻿Module Music
#Region "Dimensions"
    Public MusicOn As Boolean = False
    Public Music_FileToPlay As String
    Public MusicBaseName(9) As String
    Public MusicDecrease(9) As Short
    Public MusicIncrease(9) As Short
    Public MusicIncreaseTar(9) As Short
    Public MusicIncreaseRatio(9) As Short
    Public MusicDecreaseName(9) As String
    Public MusicIncreaseName(9) As String
#End Region
#Region "Functions"
    Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer
#End Region
    Public Sub PlayMusic(ByVal Type As String)
        If MusicOn = True Then
            If Type = "IntroMusic" Then
                MusicBaseName(0) = "IntroMusicBG"
                MusicBaseName(1) = "IntroMusicOT"
                Array.Clear(MusicDecrease, 0, MusicDecrease.Length)
                Array.Clear(MusicIncrease, 0, MusicDecrease.Length)
                Music_FileToPlay = (Chr(34) + (MainProgram.DirMusic + "Intro.mp3") + Chr(34))
                mciSendString("open " & Music_FileToPlay & " alias " + MusicBaseName(0), Nothing, 0, 0)
                mciSendString("play " + MusicBaseName(0) + " repeat", Nothing, 0, 0)
                Music_FileToPlay = (Chr(34) + (MainProgram.DirMusic + "Tango (Isaac Albéniz).mp3") + Chr(34))
                mciSendString("open " & Music_FileToPlay & " alias " + MusicBaseName(1), Nothing, 0, 0)
                mciSendString("play " + MusicBaseName(1), Nothing, 0, 0)
            ElseIf Type = "LoginMusic" Then
                MusicDecrease(0) = 1000
                MusicDecrease(1) = 1000
                MusicDecreaseName(0) = "IntroMusicOT"
                MusicDecreaseName(1) = "IntroMusicBG"
                MusicBaseName(2) = "LoginCrowd"
                Music_FileToPlay = (Chr(34) + (MainProgram.DirMusic + "LoginScreen.mp3") + Chr(34))
                mciSendString("open " & Music_FileToPlay & " alias " + MusicBaseName(2), Nothing, 0, 0)
                mciSendString("play " + MusicBaseName(2) + " repeat", Nothing, 0, 0)
                MusicIncrease(2) = 1000
                MusicIncreaseTar(2) = 300 '30% volume
                MusicIncreaseRatio(2) = 2
                MusicIncreaseName(2) = MusicBaseName(2)
            ElseIf Type = "EnvironmentMusic" Then
                'here is the natural music location where one would assess the environment and play the correct music file
                'associated with that environment.
                MusicDecrease(2) = 300
                MusicDecreaseName(2) = "LoginCrowd"
                MusicBaseName(3) = "Dungeon"
                Music_FileToPlay = (Chr(34) + (MainProgram.DirMusic + "Dungeon2.mp3") + Chr(34))
                mciSendString("open " & Music_FileToPlay & " alias " + MusicBaseName(3), Nothing, 0, 0)
                mciSendString("play " + MusicBaseName(3) + " repeat", Nothing, 0, 0)
                MusicIncrease(3) = 1000
                MusicIncreaseTar(3) = 400 '30% volume
                MusicIncreaseRatio(3) = 2
                MusicIncreaseName(3) = MusicBaseName(3)
            Else

            End If
        End If
    End Sub
    Public Sub MusicTick()
        Dim CurrentChannel As Short = 0
        If MusicOn = True Then
            For CurrentChannel = 0 To 9 Step 1
                If MusicDecrease(CurrentChannel) > 0 Then 'if decrease is set above 0 then the volume decreases gradually to zero
                    MusicDecrease(CurrentChannel) -= 10
                    'the following if statement checks to see if the current channel has both fade in and fade out signatures on it
                    'and if it does have both signatures, it closes the fade in signature and gives the fadeout signature the current
                    'volume value as teh fade in, so that it doesn't grow any more louder, and begins to decrease at the amount it's 
                    'currently at.
                    If 1000 - MusicIncrease(CurrentChannel) < MusicDecrease(CurrentChannel) And MusicIncrease(CurrentChannel) > 0 Then
                        MusicDecrease(CurrentChannel) = 1000 - MusicIncrease(CurrentChannel)
                        MusicIncrease(CurrentChannel) = 0
                    End If
                    mciSendString("setaudio " + MusicDecreaseName(CurrentChannel) + " volume to " + LTrim(Str(MusicDecrease(CurrentChannel))), Nothing, 0, 0)
                    If MusicDecrease(CurrentChannel) = 0 Then 'when the volume is muted, close channel as it was faded out
                        mciSendString("close " + MusicDecreaseName(CurrentChannel), Nothing, 0, 0)
                    End If
                ElseIf MusicIncrease(CurrentChannel) > 0 Then 'if increase is below maximum then the volume increases gradually to maximum
                    MusicIncrease(CurrentChannel) -= MusicIncreaseRatio(CurrentChannel)
                    mciSendString("setaudio " + MusicIncreaseName(CurrentChannel) + " volume to " + LTrim(Str(1000 - MusicIncrease(CurrentChannel))), Nothing, 0, 0)
                End If
                'check to see if the fade in has reached it's target amount, and if it has then quit fading in
                If MusicIncreaseTar(CurrentChannel) = 1000 - MusicIncrease(CurrentChannel) Then
                    MusicIncrease(CurrentChannel) = 0
                End If
            Next
        Else
            'insert checks here to ensure all music is turned off
            For CurrentChannel = 0 To 9 Step 1
                If MusicBaseName(CurrentChannel) <> "" Then
                    mciSendString("setaudio " + MusicBaseName(CurrentChannel) + " volume to 0", Nothing, 0, 0)
                End If
            Next
        End If
    End Sub
End Module
